#include "systemutils.h"
#include "colorutils.h"
#include "ws2812.h"
#include <QFile>
#include <QTimer>
#include <QObject>
#include <QQmlContext>
#include <QQmlProperty>
#include <QTextStream>

using colorutils::rgbaToRGB;

static const QString gpuLoadFile = "/sys/class/misc/mali0/device/utilisation";

SystemUtils::SystemUtils(QObject *parent) : QObject(parent)
{
    ws2812::ws2812b_init();
}

QString SystemUtils::gpuLoadRead(void) {
    QFile file(gpuLoadFile);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return "";
    QTextStream gpuRead(&file);

    return gpuRead.readLine(1);
}

void SystemUtils::ledaControl(const QString& value) {
    qDebug() << "LED value: " << value;
    rgbaConvert(value.toInt());
}

void SystemUtils::qmlprint(void) {
    qDebug() << "slider";
}

QString SystemUtils::rgbaConvert(int color)
{
    int colorrgb = rgbaToRGB(0,0,0, 255, 0, 0, float(color) / 255);
    qDebug() << "RGB HEX: " <<colorrgb << "Alpha: " << float(color) / 255;

    ws2812::write_color(colorrgb);

    return "";
}
